import { defineEventHandler, setResponseHeader, setResponseStatus, getRequestURL } from 'h3'; import { createPageHandler } from '@beatzball/litro/runtime/create-page-handler.js'; import { normalizePathname } from '@beatzball/litro'; import type { LitroRoute } from '@beatzball/litro'; import { routes, pageModules } from '#litro/page-manifest'; /** * Escapes text for an HTML element or a quoted attribute. `&` goes first, so * the entities the other four write are not escaped a second time. The 404 * page below writes the request path with it (issue 295): the path comes * from the client, and only escaping keeps it text. */ function escapeHtml(text: string): string { return text .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"') .replace(/'/g, '''); } function matchRoute( pathname: string, ): { route: LitroRoute; params: Record } | undefined { for (const route of routes) { if (route.isCatchAll) return { route, params: {} }; if (!route.isDynamic) { if (pathname === route.path) return { route, params: {} }; continue; } // Only the three param forms become capture groups. Every literal run // between them is escaped, so a bracket or a dot in a static part of // the path matches itself instead of acting as regex syntax. Unescaped, // '/@[handle]/:id' matched '/@h/123' as a character class (issue 234). const regexStr = '^' + route.path .split(/(:[^/]+)/) .map((part, i) => i % 2 === 1 ? part .replace(/^:([^/]+)\(\.\*\)\*$/, '(?<$1>.+)') // :param(.*)* — catch-all .replace(/^:([^/?]+)\?$/, '(?<$1>[^/]*)?') // :param? — optional .replace(/^:([^/]+)$/, '(?<$1>[^/]+)') // :param — required : part.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), ) .join('') + '$'; try { const match = pathname.match(new RegExp(regexStr)); if (match) return { route, params: (match.groups ?? {}) as Record }; } catch { // malformed pattern — skip } } return undefined; } export default defineEventHandler(async (event) => { // Canonicalize before matching: '/docs/a/' and '/docs/a' are one page. // The client router strips the same trailing slash, so both halves agree // (issue 203). A mismatch renders a blank 200 page, not an error. const pathname = normalizePathname(getRequestURL(event).pathname); const result = matchRoute(pathname); if (!result) { // A miss has to say 404 in the status, not only in the body. Without // this the 404 page goes out 200 OK, so crawlers index it and a // prerender counts it as a real page. setResponseStatus(event, 404); setResponseHeader(event, 'content-type', 'text/html; charset=utf-8'); return ` 404

404 — Not Found

No page matched ${escapeHtml(pathname)}.

`; } const { route: matched, params } = result; event.context.params = { ...event.context.params, ...params }; const handler = createPageHandler({ route: matched, pageModule: pageModules[matched.filePath], }); return handler(event); });